package com.xhj.es.controller;

import com.xhj.es.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ContentController {

    @Autowired
    private ContentService contentService;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @ResponseBody
    @GetMapping("/parse/{keyword}")
    public boolean parseContent(@PathVariable String keyword){
        return contentService.parseContent(keyword);
    }

    @ResponseBody
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String,Object>> searchPage(@PathVariable String keyword,
                                               @PathVariable(required = false) Integer pageNo,
                                               @PathVariable(required = false) Integer pageSize){
        if (pageNo == null) pageNo = 0;
        if (pageSize == null) pageSize = 10;
        boolean parseContent = contentService.parseContent(keyword);
        if (!parseContent) return new ArrayList<Map<String,Object>>();
        return contentService.searchPage(keyword,pageNo,pageSize);
    }
}
